import Vue from 'vue'
import Router from 'vue-router'
import { setComponent } from '@/utils/index'
Vue.use(Router)

/* Layout */
import Layout from '@/views/layout/Layout'

/** note: sub-menu only appear when children.length>=1**/
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/authredirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/errorPage/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/errorPage/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: 'dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'dashboard', icon: 'dashboard', noCache: true, affix: true }
      }
    ]
  }
]
// 静态
export default new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

// 动态
export const asyncRoutes = setRoute() || []
function setRoute() {
  const route = JSON.parse(localStorage.getItem('menu')) || []
  // console.log(route)
  if (route) {
    const a = setComponent(route)
    a.push({ path: '*', redirect: '/404', hidden: true })
    a.forEach(item => {
      if (item.name === 'setting') {
        item.children.push({
          path: 'report',
          name: 'report',
          component: () => import('@/views//setting/report'),
          meta: { 'title': '举报' }
        })
      }
    })
    return a
  }
}
